//
//  TranslationPictureResultView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/5/9.
//

import UIKit
import Reusable

extension TranslationPictureResultView: NibLoadable {}

class TranslationPictureResultView: UIView {
    
    @IBOutlet private weak var collectionView: UICollectionView!
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet private weak var saveButton: UIButton!
    private var list: [TranslationPictureResultItem] = []
    
    private lazy var customPageControl: YLDotPageControl = {
        let pageControl = YLDotPageControl()
        pageControl.backgroundColor = .clear
        pageControl.currentPage = 0
        pageControl.dotSize = 8
        // 选中圆点高度将始终保持这个值
        pageControl.activeDotSize = 16
        pageControl.dotColor = .white
        pageControl.activeDotColor = .themeColor
        pageControl.spacing = 12
        
        return pageControl
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    func addItem(_ item: TranslationPictureResultItem) {
        list.append(item)
        customPageControl.numberOfPages = list.count
        collectionView.reloadData()
    }
    
    func cleanAll() {
        list = []
        customPageControl.numberOfPages = 0
        collectionView.reloadData()
    }
    
    @IBAction func saveAction(_ sender: Any) {
        let index = customPageControl.currentPage
        if list.count > index {
            // 存在
            let item = list[index]
            item.image.sg.saveImageToPhotoAlbum { isSuccess in
                if isSuccess {
                    xLog("保存图片")
                    YLHUD.showMessage("保存图片到相册")
                    
                } else {
                    xLog("保存图片失败")
                }
                
            }
        }
    }
}


private extension TranslationPictureResultView {
    func setupUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.main.bounds.size
        
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.isPagingEnabled = true
        collectionView.isScrollEnabled = true
        collectionView.bounces = false
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(cellType: TranslationPictureResultItemCell.self)
        
        addSubview(customPageControl)
        customPageControl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(resetButton.snp.top).offset(-16)
            make.height.equalTo(16)
        }
        
        // 配置自定义PageControl
        customPageControl.backgroundColor = .clear
        customPageControl.currentPage = 0
        customPageControl.dotSize = 8
        customPageControl.activeDotSize = 16
        customPageControl.dotColor = .white
        customPageControl.activeDotColor = .themeColor
        customPageControl.spacing = 12
        
        
        
    }
}

extension TranslationPictureResultView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: TranslationPictureResultItemCell.self)
        let model = list[indexPath.row]
        cell.imageView.image = model.image
        cell.typeLabel.text = model.type == 0 ? "原图".localized(.tran) : "译文".localized(.tran)
        return cell
    }
}

// MARK: - ScrollView 代理方法
extension TranslationPictureResultView: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageIndex = Int(round(scrollView.contentOffset.x / scrollView.bounds.width))
        customPageControl.currentPage = pageIndex
    }
}

struct TranslationPictureResultItem {
    /// 类型, 0:原图  1:译文,结果图
    var type: Int
    var image: UIImage
    
    init(type: Int, image: UIImage) {
        self.type = type
        self.image = image
    }
}
